const app = getApp()
const api = require('../../../api/index.js')
var util = require("../../../utils/util.js");
const tokenState = getApp().messages();
// let nums = 1 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    note_id:'',
    noData: true,
    page:1,
    address: [],
    removeData:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      note_id: options.id,
      cont: options.conten
    })
    let data ={
      note_id: options.id,
      page:1
    }
    this.allcomments(data)
  },
  allcomments:function(data){
    api.commentList(data,(res) =>{
      console.log(res)
      this.setData({
        address: res.data.result
      })
    })
  },
  btnText:function(e){
    console.log(e.detail.value);
    let val = e.detail.value
    let id = this.data.note_id
    let data ={
      note_id: id,
      name: val
    }
    if (val != ''){
      api.comment(data, (res) => {
        console.log(res)
        this.setData({
          searchinput: '',
        })
        wx.showToast({
          title: '评论成功',
          icon: "none"
        })
      })
    }
    
  },
  Delete: function (e) {
    let myCYID = e.currentTarget.dataset.id
    console.log(e)
    this.setData({
      setId: myCYID,
      removeData: true
    })
  },
  DeleteR: function () {
    let comment_id = this.data.setId
    let data = {
      comment_id: comment_id
    }
    let that = this
    api.commentRemove(data, (res) => {
      if (res.data.code == 200) {
        let data = {
          note_id: this.data.note_id,
          latitude: wx.getStorageSync('key').latitude,
          longitude: wx.getStorageSync('key').longitude
        }
        
        this.allcomments(data)
        this.setData({
          removeData:false
        })
        wx.showToast({
          title: '删除成功',
          duration: 1000
        });

      } else {
        wx.showToast({
          title: '删除失败稍后再试试哦',
          duration: 1000
        });
      }

    })
  },
  cancel: function () {
    this.setData({
      removeData: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  
  onReachBottom: function () {
     this.data.page += 1;
   

    let that = this;
    let data ={
      note_id: this.data.note_id,
      page: this.data.page
    }
    console.log(this.data.page)
    if (this.data.noData == true){
      api.commentList(data, (res) => {
        this.setData({
          noData: false
        })
        console.log(res)
        if (res.data.result.length > 0) {
          console.log('走没有')
          this.setData({
            address: this.data.address.concat(res.data.result),
            noData: true
          })
        } else {
          this.data.noMore = true;
          wx.showToast({
            title: '没有数据了',
            icon: "none"
          })
          that.setData({
            noData:false
          })
        }
      })
    }else{
      wx.showToast({
        title: '没有数据了',
        icon: "none"
      })
    }   
  },

})